//引入数据结构
const { TypInModel } = require(`../modules/payTypInModel`);
const { ownerModel } = require('../modules/ownerModel');
const { paySetModel } = require('../modules/paySetModel');
//新增项目名称
async function addpayTypIn(req, res) {
    try {
        const data = req.body;
        await TypInModel.create(data);
        res.send({
            message: '新增成功',
            code: 200
        })
    } catch (error) {
        console.log(`新增失败--------------`, error);
        res.send({
            message: '新增失败',
            code: 500
        })
    }
}

// 得到项目标准
async function getTypIn(req, res) {
    try {
        const { nowPage, pageSize, value, cmd } = req.body;
        // console.log(`前端返回的val=============`, req.body);

        const total = await TypInModel.countDocuments({
            $or: [
                {
                    time: {
                        $regex: value,
                        $options: `$i`
                    }
                },
                {
                    chargeState: {
                        $regex: value,
                        $options: `$i`
                    }
                }
            ]
        })
        const pages = Math.ceil(total / pageSize);  //总共的页数是 总的数据条数/一页展示多少条  然后向上取整 多的显示一页
        const data = await TypInModel
            .find(
                {
                    $or: [
                        {
                            time: {
                                $regex: value,
                                $options: `$i`
                            }
                        },
                        {
                            chargeState: {
                                $regex: value,
                                $options: `$i`
                            }
                        }
                    ]
                })//查询符合条件的所有数据
            .populate("communtiyId setNameId room")
            .limit(pageSize * 1) //只请求一页需要展示的数据
            .skip((nowPage * 1 - 1) * pageSize); //需要跳过的数据 在第一页的时候 跳过0条 第二页的时候 跳过第一页所需要的数据
        // console.log(`所有的项目名称-------------------`, data);
        res.send({
            pages,
            total,
            data,
            message: '获取到所有项目typein',
            code: 200
        })
    } catch (error) {
        console.log(`得到失败--------------`, error);
        res.send({
            message: '得到失败',
            code: 500
        })
    }
}


//删除
async function delTypIn(req, res) {
    try {
        const data = req.body;
        // console.log(`前端返回的val=============`, value);

        await TypInModel.deleteOne(data);
        const total = await TypInModel.countDocuments();
        res.send({
            total,
            message: '删除成功',
            code: 200
        })



    } catch (error) {
        console.log(`报错了------------------------`, error);
        res.send({
            message: '后端报错了',
            code: 500
        })
    }
}

//修改
async function changeTypIn(req, res) {
    try {
        const { communtiyId, room, setNameId, count, time, startKey, endKey, shouldPay, prestore, chargeState, _id } = req.body;
        // console.log(`前端返回修改的值====================`, data);
        await TypInModel.updateOne({ _id }, { communtiyId, room, setNameId, count, time, startKey, endKey, shouldPay, prestore, chargeState })

        res.send({
            message: `修改成功`,
            code: 200
        })


    } catch (error) {
        console.log(`获取失败--------------`, error);
        res.send({
            message: '获取失败',
            code: 500
        })
    }
}

//查找数据
async function findTypIn(req, res) {
    try {
        const resulte = req.body;

        const data = await TypInModel
            .find(resulte)
            .populate("communtiyId setNameId room");

        if (data.length === 1) {
            res.send({
                data,
                message: '获取成功',
                code: 200
            })
        } else {
            res.send({
                message: '获取失败 该数据可能被删除',
                code: 500
            })
        }




    } catch (error) {
        console.log(`报错了------------------------`, error);
        res.send({
            message: '后端报错了',
            code: 500
        })
    }
}

//查找多个库的数据
async function findMoreTypIn(req, res) {
    try {
        const { type, value, nowPage, pageSize } = req.body;

        const [{ _id }] = await ownerModel.find({ $or: [{ [type]: value }] }
        )
        if (_id) {
            const data = await TypInModel
                .find({
                    room: _id,
                    chargeState: "未缴费"
                })
                .populate("communtiyId setNameId room")
                .limit(pageSize * 1)
                .skip((nowPage * 1 - 1) * pageSize);
            const total = data.length;
            const pages = Math.ceil(total / pageSize);
            res.send({
                total,
                pages,
                data,
                message: '获取成功',
                code: 200
            })
            // console.log(`more===========================`, data);
        }
    } catch (error) {
        console.log(`报错了------------------------`, error);
        res.send({
            message: '后端报错了',
            code: 500
        })
    }
}

//批量修改状态
async function AllchangeTypIn(req, res) {
    try {

        const resulte = req.body;
        if (resulte.cmd) {
            console.log(`@@@@@@@@`, resulte);
            await TypInModel.updateOne({ _id: resulte._id }, { prestore: resulte.prestore })
        } else {
            await TypInModel.updateMany(resulte, { chargeState: `已缴费` })
            console.log(`======================================`, resulte);


        }
        res.send({

            message: '获取成功',
            code: 200
        })





    } catch (error) {
        console.log(`报错了------------------------`, error);
        res.send({
            message: '后端报错了',
            code: 500
        })
    }
}

//搜索
async function search(req, res) {
    const { nowPage, pageSize, value, cmd } = req.body;
    let data1 = await paySetModel
        .find(
            {
                $or: [
                    {
                        projectName: {
                            $regex: value,
                            $options: `$i`
                        }
                    },
                    {
                        payClassName: {
                            $regex: value,
                            $options: `$i`
                        }
                    },
                    {
                        paySetName: {
                            $regex: value,
                            $options: `$i`
                        }
                    }
                ]

            })//查询符合条件的所有数据
        .populate("communtiyId");

    data1 = data1.map(item => item._id)
    let data = []
    for (let i = 0; i < data1.length; i++) {
        data.push(await TypInModel.find({ setNameId: data1[i] }))
    }
    console.log(`111111111111111111111111111111111`, data);
}





module.exports = {
    addpayTypIn, getTypIn, delTypIn, changeTypIn, findTypIn, findMoreTypIn, AllchangeTypIn
}